package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName CheckItemController
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/8/20 16:03
 * @Version V1.0
 */
@RestController
@RequestMapping(value = "/permission")
public class PermissionController {

    @Reference
    PermissionService permissionService;
    // 查询检查组列表
    @RequestMapping(value = "/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        PageResult pageResult = permissionService.findPage(queryPageBean.getQueryString(),
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize());
        return pageResult;

    }
    // 新增权限（返回 public Result(boolean flag, String message)）
    @RequestMapping(value = "/add")
    public Result add(@RequestBody Permission permission){
        try {
            permissionService.add(permission);
            // 成功
            return new Result(true, "添加权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            // 失败
            return new Result(false, "添加权限失败");
        }
    }
    // 根据id，删除检查项
    @RequestMapping(value = "/delete")
//    @PreAuthorize(value = "hasAuthority('MENU_DELETE123')")
    public Result delete(Integer id){
        try {
            permissionService.deleteById(id);
            return new Result(true,"删除权限成功");
        }catch (RuntimeException e){ // 遇到运行时异常，表示检查项和检查组的关联表中存在数据
            return new Result(false,e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除权限失败");
        }
    }

    // 使用id查询检查项
    @RequestMapping(value = "/findById")
    public Result findById(Integer id){
        try {
            Permission permission = permissionService.findById(id);
            return new Result(true,"回显权限成功",permission);
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"回显权限失败");
        }
    }

    // 使用id查询检查项
    @RequestMapping(value = "/edit")
//    @PreAuthorize(value = "hasAuthority('CHECKITEM_EDIT')")
    public Result edit(@RequestBody Permission permission){
        System.out.println(permission.getDescription());
        try {
            permissionService.edit(permission);
            return new Result(true,"修改权限成功");
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改权限失败");
        }
    }

}
